<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;
use think\Db;
use app\common\model\UserAccount;
/**
 * Work逻辑
 */
class Work extends LogicBase
{
    

    
    /**
     * 获取Work列表
     */
    public function getWorkList($data,$where = [], $field = 'a.*', $order = '')
    {

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $list = $this->modelChannelWorkContent->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate(10, false, ['query' => $data]);
        $count=$this->modelChannelWorkContent->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();

        return ['count'=>$count,'list'=>$list];
    }
    
    /**
     * 获取Work列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];

        !empty($data['calss']) && $where['a.class'] = $data['class'];
        !empty($data['search_data']) && $where['a.title|a.content|c.username'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['time1']) && $where['a.create_time'] =['gt',strtotime($data['time1'].' 00:00:00')];
        !empty($data['time2']) && $where['a.create_time'] =['lt',strtotime($data['time2'].' 23:59:59')];
        return $where;
    }
    
    /**
     * Work信息编辑
     */
    public function WorkEdit($data = [])
    {
        
//        $validate_result = $this->validateWork->scene('edit')->check($data);
//
//        if (!$validate_result) {
//
//            return [RESULT_ERROR, $this->validateWork->getError()];
//        }
//
        $url = url('WorkList');
        if(!empty($data['content'])){
            $this->modelChannelWorkContent->setInfo(['id'=>$data['id'],'state'=>2]);
            $admin_id=session('member_info.id');
            $dat=[
                'wid'=>$data['id'],
                'is_admin'=>1,
                'user_id'=>$admin_id,
                'admin_name'=>session('member_info.username'),
                'content'=>$data['content']
            ];
            $this->modelChannelWorkComment->setInfo($dat);
        }


        return [RESULT_SUCCESS, '操作成功', $url];
    }

    /**
     * 获取Work信息
     */
    public function getWorkInfo($where = [], $field = 'a.*')
    {
        
        $this->modelChannelWorkContent->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelChannelWorkContent->getInfo($where, $field);
    }

    /**

    
    /**
     * Work删除
     */
    public function WorkDel($where = [])
    {
        
        $result = $this->modelChannelWorkContent->deleteInfo($where);
        
        $result && action_log('删除', 'Work删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'Work删除成功'] : [RESULT_ERROR, $this->modelChannelWorkContent->getError()];
    }

    /**
     * 获取分类信息
     */
    public function getWorkCategoryInfo($where = [], $field = true)
    {

        return $this->modelChannelWorkClass->getInfo($where, $field);
    }

    /**
     * 获取工单分类列表
     */
    public function getWorkCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        $list=$this->modelChannelWorkClass->getList($where, $field, $order, $paginate);
        $count=$this->modelChannelWorkClass->where($where)->count();
        return ['count'=>$count,'list'=>$list];

    }

    /**
     * 工单分类删除
     */
    public function WorkCategoryDel($where = [])
    {

        $result = $this->modelChannelWorkClass->deleteInfo($where);

        $result && action_log('删除', '工单分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '工单分类删除成功'] : [RESULT_ERROR, $this->modelChannelWorkClass->getError()];
    }

    /**
     * 工单分类编辑
     */
    public function WorkCategoryEdit($data = [])
    {

        $validate_result = $this->validateTicket->scene('class')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateTicket->getError()];
        }

        $url = url('WorkCategoryList');

        $result = $this->modelChannelWorkClass->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '工单分类' . $handle_text . '，name：' . $data['classname']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelChannelWorkClass->getError()];
    }


    /**
     * 结束工单
     */
    public function update_info($data)
    {

        //dd($where);
        $result=$this->modelChannelWorkContent->setInfo(['id'=>$data['id'],'state'=>4]);
//        $result = $this->modelChannelWorkContent->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
    
}
